package com.xcn.test;

import java.io.*;
import java.util.*;



/**
 * Update Property Setting
 * 원본 Property 파일과 비교하여 해당 파일을 최신으로 변경 한다.
 * 
 * @author changmin
 * @since  2013-05-30
 */
public class UpdateProperty
{

	public UpdateProperty( )
	{
		super ( );
	}

	/**
	 * Null to Empty String
	 * @param target
	 * @return
	 */
	public static String objFixNull ( Object target )
	{
		String result = "";
		if ( target != null )
		{
			if ( !String.valueOf ( target ).equals ( "null" ) ) return String.valueOf ( target );
		}
		return result;
	}

	/**
	 * 
	 * @param path
	 * @return
	 */
	public List<Map<String,String>> getProperties ( String path )
	{
		List<Map<String,String>> result = new ArrayList <Map<String,String>> ( );

		LinkedProperties props = null;
		FileInputStream  fis = null;
		try
		{
			File _file = new File ( path );
			if ( _file.isFile ( ) )
			{
				props = new LinkedProperties ( );
				
				fis = new FileInputStream ( _file );
				props.load ( fis );
				Enumeration <Object> enumeration = props.keys ( );
				while( enumeration.hasMoreElements ( ) )
				{
					Object keyObj = enumeration.nextElement ( );
					String key = objFixNull ( keyObj );
					if ( !key.isEmpty ( ) )
					{
						Map <String, String> item = new HashMap <String, String> ( );
						item.put ( "key", key );
						item.put ( "value", new String( objFixNull ( props.get ( key ) ).getBytes ( "ISO-8859-1" ), "utf-8" ) );

						result.add ( item );
					}
				}
			}
		}
		catch ( FileNotFoundException e )
		{
			e.printStackTrace ( );
		}
		catch ( Exception e )
		{
			e.printStackTrace ( );
		}
		finally
		{
			try { if ( fis != null ) fis.close ( ); } catch (Exception e) { e.printStackTrace ( ); } //파일에 대한 자원 해제.
		}

		return result;
	}

	private void execute ( String newPath, String oldPath, String applyPath )
	{
		LinkedProperties props = new LinkedProperties ( );
		

		StringBuffer _sb = new StringBuffer ( );
		List <Map <String, String>> _new = getProperties ( newPath );
		List <Map <String, String>> _old = getProperties ( oldPath );
		for ( int i = 0 ; i < _new.size ( ) ; i++ )
		{
			Map <String, String> newItem = _new.get ( i );
			String newKey = newItem.get ( "key" );

			boolean appendFlag = false;
			for ( int j = 0 ; j < _old.size ( ) ; j++ )
			{
				Map <String, String> oldItem = _old.get ( j );
				String oldKey = oldItem.get ( "key" );
				if ( newKey.equals ( oldKey ) )
				{
					String oldValue = oldItem.get ( "value" );
					_sb.append ( newKey ).append ( "=" ).append ( oldValue ).append ( "\n" );

					props.put ( newKey, oldValue );
					appendFlag = true;
				}
			}

			if ( !appendFlag )
			{
				String newValue = newItem.get ( "value" );
				
				props.put ( newKey, newValue );

				_sb.append ( newKey ).append ( "=" ).append ( newValue ).append ( "\n" );
			}
		}
		
		FileOutputStream out = null;
		try
		{
			out = new FileOutputStream ( new File ( applyPath ) );
			props.store ( out, "Property Setup\nasddf" );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
		}
		finally
		{
			try { if ( out != null ) out.close ( ); } catch (Exception e) { e.printStackTrace ( ); } //파일에 대한 자원 해제.
		}

		System.out.println ( _sb.toString ( ) );
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main ( String[] args ) throws Exception
	{
		if ( args.length != 3 )
		{
			throw new Exception ( "환경설정 파일 복원에 실패 하였습니다." ); 
		}

		UpdateProperty _up = new UpdateProperty ( );
		_up.execute ( args[0], args[1], args[2] );
	}
	
}
